package com.ruoyi.core.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.entity.CoreEntity.StockLockRecord;
import com.ruoyi.common.entity.CoreEntity.StockTransactionRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.core.mapper.StockLockRecordMapper;

import com.ruoyi.core.service.IStockLockRecordService;
import com.ruoyi.core.service.IStockTransactionRecordService;

/**
 * 库存锁定记录Service业务层处理
 *
 * 当锁定记录状态更改为已出库（3）时，生成一条销售出库交易记录并自动扣减库存
 *
 * @author yfk
 * @date 2025-08-07
 */
@Service
public class StockLockRecordServiceImpl implements IStockLockRecordService
{
    @Autowired
    private StockLockRecordMapper stockLockRecordMapper;

    @Autowired
    private IStockTransactionRecordService stockTransactionRecordService;

    @Override
    public StockLockRecord selectStockLockRecordById(Long id)
    {
        return stockLockRecordMapper.selectStockLockRecordById(id);
    }

    @Override
    public List<StockLockRecord> selectStockLockRecordList(StockLockRecord stockLockRecord)
    {
        return stockLockRecordMapper.selectStockLockRecordList(stockLockRecord);
    }

    @Override
    public int insertStockLockRecord(StockLockRecord stockLockRecord)
    {
        stockLockRecord.setCreateTime(DateUtils.getNowDate());
        return stockLockRecordMapper.insertStockLockRecord(stockLockRecord);
    }

    @Override
    public int updateStockLockRecord(StockLockRecord stockLockRecord)
    {
        // 查询旧数据
        StockLockRecord oldRecord = stockLockRecordMapper.selectStockLockRecordById(stockLockRecord.getId());

        int rows = stockLockRecordMapper.updateStockLockRecord(stockLockRecord);

        // 如果状态从非已出库变为已出库(3)
        if (oldRecord != null
                && (oldRecord.getLockStatus() == null || oldRecord.getLockStatus() != 3)
                && stockLockRecord.getLockStatus() != null
                && stockLockRecord.getLockStatus() == 3) {

            StockTransactionRecord transaction = new StockTransactionRecord();
            transaction.setStoreInfoId(stockLockRecord.getStoreInfoId());
            transaction.setSkuId(stockLockRecord.getSkuId());
            transaction.setRelatedOrderNo(stockLockRecord.getOrderNo());
            transaction.setQuantity(stockLockRecord.getLockQuantity());
            transaction.setTransactionType(2L); // 2-销售出库
            transaction.setSourceType(2L);      // 来源订单
            transaction.setDelFlag("0");

            // 调用现有方法，自动扣减库存
            stockTransactionRecordService.insertStockTransactionRecord(transaction);
        }

        return rows;
    }

    @Override
    public int deleteStockLockRecordByIds(Long[] ids)
    {
        return stockLockRecordMapper.deleteStockLockRecordByIds(ids);
    }

    @Override
    public int deleteStockLockRecordById(Long id)
    {
        return stockLockRecordMapper.deleteStockLockRecordById(id);
    }
}
